//
//  YSUtility.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-10.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import "YSUtility.h"
#import "JSON/JSONKit.h"
#import "NSData+AES256.h"

@implementation YSUtility

+ (BOOL)isBlankObject:(id)object {
    if (object == nil) {
        return YES;
    }
    if (object == NULL) {
        return YES;
    }
    if ([object isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([object isEqual:[NSNull null]]) {
        return YES;
    }
    if ([object isKindOfClass:[NSString class]]) {
        if ([[object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
            return YES;
        } else {
            const char *str = [object UTF8String];
            if (strlen(str) == 0) {
                return YES;
            }
        }
    }
    return NO;
}

+ (NSArray *)returnArray:(NSArray *)array {
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger index,BOOL *stop){
        if (obj == [NSNull null]) {
            [mutableArray addObject:@""];
        } else if ([obj isKindOfClass:[NSArray class]]) {
            [mutableArray addObject:[self returnArray:array]];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            [mutableArray addObject:[self returnDictionary:obj]];
        } else {
            [mutableArray addObject:obj];
        }
    }];
    return mutableArray;
}

+ (NSDictionary *)returnDictionary:(NSDictionary *)params {
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
        if (obj == [NSNull null]) {
            [dictionary setObject:@"" forKey:key];
        } else if ([obj isKindOfClass:[NSArray class]]) {
            [dictionary setObject:[self returnArray:obj] forKey:key];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            [dictionary setObject:[self returnDictionary:obj] forKey:key];
        } else {
            [dictionary setObject:obj forKey:key];
        }
    }];
    return dictionary;
}

+ (NSString *)getGameStatusText:(eRoomStatus)status {
    if (status == eGameWait) {
        return @"等待中";
    }
    return @"游戏中";
}

+ (NSString *)getSocketInterface:(eSocketAciton)action {
    return [NSString stringWithFormat:@"%d",action];
}

+ (NSString *)encryptSocketDict:(NSDictionary *)dict {
    NSString *str = [dict JSONString];
    str = [NSData AES256EncryptWithPlainTextExt:str];
    str = [str stringByAppendingString:@"\r\n"];
    return str;
}

+ (NSInteger)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

+ (void)playSound:(NSString *)soundName soundType:(NSString *)type {
#if TARGET_IPHONE_SIMULATOR
#else
    NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:soundName ofType:type];       //创建音乐文件路径
    NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
    
    AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
    
    [thePlayer prepareToPlay];
    [thePlayer setVolume:1];   //设置音量大小
    thePlayer.numberOfLoops = 1;//设置音乐播放次数  -1为一直循环
    [thePlayer play];   //播放
#endif
}


+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
@end
